package com.bpf.base.service.system;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.bpf.base.util.MapUtil;
import com.bpf.base.dao.system.TSysMenuDao;
import com.bpf.base.model.system.TSysMenu;

/**
 * TODO 本代码由代码生成工具生成
 * @date 2016-07-19 05:31:26
 */
@Service
public class TSysMenuService{

	private Logger logger = LoggerFactory.getLogger(TSysMenuService.class);
     
    @Resource
	private TSysMenuDao tSysMenuDao;
	
	//根据条件搜索记录
	public Map<String , Object> getTSysMenuList(Map<String, Object> params){
		Map<String , Object> queryMap = new HashMap<String, Object>();
		Map<String , Object> resultMap = new HashMap<String, Object>();
		//TODO 根据需要封装查询需要的条件
		queryMap.putAll(params);
		resultMap.put("rows", tSysMenuDao.selectList(queryMap));
		resultMap.put("total", tSysMenuDao.selectCount(queryMap));
		return resultMap;
	}
       
	//新增记录
	public int addTSysMenu(Map<String, Object> params){
		try {
			//TODO 根据需要封装查询需要的条件
			TSysMenu temp=MapUtil.toObject(TSysMenu.class, params);
			return tSysMenuDao.insert(temp);
		} catch (Exception e) {
			logger.error("添加失败",e);
			return -1;
		}
	}
	
	//更新记录
	public int updateTSysMenu(TSysMenu tSysMenu){
		return tSysMenuDao.update(tSysMenu);
	}
	
	//删除记录
	public int delTSysMenu(Map<String, Object> params){
		return tSysMenuDao.deleteByMap(params);
	}
	
	//根据主键批量删除
	public int delTSysMenuBat(String[] idArray){
		for(String pk:idArray){
			tSysMenuDao.deleteByPk(pk);
		}
		return idArray.length;
	}
	
	public List<TSysMenu> getAllTSysMenu(Map<String, Object> params){
		return tSysMenuDao.selectList(params);
	}
	
	public TSysMenu selectByPrimaryKey(Integer menuId) {
		return tSysMenuDao.selectByPk(menuId);
	}
	
	public List<TSysMenu> selectList(Map<String, Object> conditions) {
		return tSysMenuDao.selectList1(conditions);
	}

	public int selectCount(Map<String, Object> conditions) {
		return tSysMenuDao.selectCount(conditions);
	}

	public List<TSysMenu> selectByParentMenuId(Integer menuParentId) {
		return tSysMenuDao.selectByParentMenuId(menuParentId);
	}

	public int selectMaxOrder(int menuParentId) {
		return tSysMenuDao.selectMaxOrder(menuParentId);

	}

	public int selectMinOrder(int menuParentId) {
		return tSysMenuDao.selectMinOrder(menuParentId);

	}

	public List<TSysMenu> selectByRoleId(Integer roleId) {
		return tSysMenuDao.selectByRoleId(roleId);
	}
	
	public List<TSysMenu> selectProByRoleId(Integer roleId){
		return tSysMenuDao.selectProByRoleId(roleId);
	}

	public List<TSysMenu> selectListInId(Map<String, Object> conditions){
		return tSysMenuDao.selectListInId(conditions);
    }
	
	public void insertSysMenu(Map<String, Object> map){
		tSysMenuDao.insertSysMenu(map);
	}
	
	/** 
	* @author 罗成
	* @date 创建时间：2016年8月4日 下午4:59:23
	* @version v1.0
	* 获得menuTree的treegrid数据
	*/ 
	public List<TSysMenu> getMenuTreeDataList(Map<String, Object> conditions) {
		return getMenuList(tSysMenuDao.selectList1(conditions));
	}
	
	public List<TSysMenu> getMenuList(List<TSysMenu> allMenu){
		List<TSysMenu> resultList = new ArrayList<TSysMenu>();
		for(int i = 0; i < allMenu.size(); i++){
			if(allMenu.get(i).getMenuParentId() == -1){
				resultList.add(allMenu.get(i));
				nodeRecursion(allMenu.get(i), allMenu);
			}
		}
		return resultList;
	}
	
	public void nodeRecursion(TSysMenu node,List<TSysMenu> tList){
		List<TSysMenu> resultList = new ArrayList<TSysMenu>();
		node.setChildren(resultList);
		for(int i=0;i<tList.size();i++){
			if(tList.get(i).getMenuParentId().equals(node.getMenuId())){
				TSysMenu node1=tList.get(i);
				resultList.add(node1);
				tList.remove(i);
				i--;
				nodeRecursion(node1,tList);
			}
		}
	}
}
